package com.example.myhelloworld.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import gwt.canvas.client.Canvas;;

public class StatisticsExercise extends ExerciseClass {

	int canvasPixelsPerValue = 4; 
	int canvasWitdth = 101*canvasPixelsPerValue; // = 4 pixels per datavalue.. 
	int canvasHeight = 400; 
	Canvas canvas;
	
	public StatisticsExercise(String uName, String settings,
			GreetingServiceAsync GSA) {
		super(uName, settings, GSA);
			
		System.out.println("Create a canvas... ");
		canvas = new Canvas(canvasWitdth,canvasHeight);
		System.out.println("new vertical panel, add a button to that: ... ");
		selfPanel = new VerticalPanel();
		selfPanel.addStyleName("ExerciseBasic");
		
		produceResultButton = new Button("Close");
		selfPanel.add(produceResultButton);
		System.out.println("... ");
		selfPanel.add(canvas);

		gsa.getExerciseNames(new AsyncCallback<String>(){

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(String result) {
				HorizontalPanel buttonrow = new HorizontalPanel();
				selfPanel.add(buttonrow);
				String[] exercises = result.split(",");
				for (int i = 0; i<exercises.length; i++){
					Button exercisei = new Button(exercises[i]);
					buttonrow.add(exercisei);
					exercisei.addClickHandler(new ClickHandler(){

						public void onClick(ClickEvent event) {
							gsa.getExerciseResults(((Button)event.getSource()).getText(), new AsyncCallback<String>(){

								public void onFailure(Throwable caught) {		
								}

								public void onSuccess(String result) {
									System.out.println("Got a response back, about to draw in canvas with following argument: "+ result);
									drawInCanvas(result);
								}});
						}});
				}
			}}); 
	}

	void drawInCanvas(String arrayToPrint){
		int[] resultArray = new int[101];
		int[] randomResultArray = new int[101];
 		String[] arrayStrints = arrayToPrint.split(",");
// 		System.out.println("Number of comma.separated elements in list: " + arrayStrints.length);
 		int arraySum = 0;
 		int maxValue = 0;
 		int randMaxValue = 0;
 		int pixelsPerDataValue;
 		for (int i = 1; i < arrayStrints.length; i++){
 			int thisIndexValue = Integer.parseInt(arrayStrints[i]);
 			int thatIndexValue = (int)(Math.random()*40);		//randomizes results just for testing
 			resultArray[i-1] = thisIndexValue;
 			randomResultArray[i-1] = thatIndexValue;
 			arraySum = arraySum + thisIndexValue;
 			if (thatIndexValue > randMaxValue){
 				randMaxValue = thatIndexValue;
 			}
 			
 			if (thisIndexValue > maxValue){
 				maxValue = thisIndexValue;
 			}
 		}
 		
 		if (maxValue == 0){		//If the max-value is 0, that means that no statistics have been reported in. That is a good case for when to randomize statistics, just to show what the statistics can do..
 			resultArray = randomResultArray;
 			maxValue = randMaxValue;
 			System.out.println("SHOWING RANDOM STATISTICS!");
 		}
 		
 		canvas.clear();
 		int heightPixelsPerDataValue = canvasHeight / maxValue ;
 		for (int j = 0; j<101; j++){
// 			System.out.println("Number in loop: " + j);
 			try{
 				double heightBarGraph = heightPixelsPerDataValue*resultArray[j];
 				canvas.fillRect(j*canvasPixelsPerValue, canvasHeight-heightBarGraph, canvasPixelsPerValue, heightBarGraph);
// 				System.out.println("Draws a rectangle with following parameters: " + (j*canvasPixelsPerValue) + " : " + (canvasHeight-heightBarGraph) + " : " + canvasPixelsPerValue + " : " + heightBarGraph);
 				
 			}catch(Exception e){}
 		}

	}
	
	@Override
	public void draw() {
	}

	@Override
	public String pushresult() {
		return "";
	}

}
